package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;


import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.TextView;

import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.zyj.myapplication.Fragment.DashiClassInfoFragment;
import com.zyj.myapplication.Fragment.DashiWentiInfoFragment;
import com.zyj.myapplication.Javabean.CoachInfo;
import com.zyj.myapplication.Model.CoachModel;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;


import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CoachListActivity extends FragmentActivity implements View.OnClickListener {
    private TextView TabKt;
    private TextView TabWt;
    private ImageView coachPhoto, btn_back,collect;
    private TextView coachIntro, coachName;
    private String Id;
    private int userid;
    private Fragment Tab01;
    private Fragment Tab02;
    private String coach_id;
    private CoachInfo coachinfo;
    private Context mContext;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach_list);
        initViews();
        initEvents();
        setSelect(0);
        initData();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void initViews() {
        coachPhoto = (ImageView) findViewById(R.id.coachPhoto);
        coachIntro = (TextView) findViewById(R.id.coachIntro);
        coachName = (TextView) findViewById(R.id.coach_name);
        collect = (ImageView)findViewById(R.id.guanzhu);
        btn_back = (ImageView) findViewById(R.id.coach_back);
        TabKt = (TextView) findViewById(R.id.coachkt);
        TabWt = (TextView) findViewById(R.id.coachwt);
        mContext = CoachListActivity.this;
    }

    private void initEvents() {
       /* btn_class.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent (CoachListActivity.this,DashiClassInfoFragment.class);
                startActivity(intent);
            }

        });
        btn_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent (CoachListActivity.this,DashiWentiInfoFragment.class);
                startActivity(intent1);
            }
        });*/

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });


        TabKt.setOnClickListener(this);
        TabWt.setOnClickListener(this);


    }

    private void initData() {
        Intent intent = getIntent();
        coach_id = intent.getStringExtra("coach_id");
        userid = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);
        getTeaState();
        /*
        tea_collect = (ImageView) findViewById(R.id.guanzhu);
        tea_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });*/
        showData();
    }

    private void showData() {
        CoachModel coachModel = new CoachModel();
        Call<CoachInfo> call = coachModel.getCoachinfo(coach_id);
        call.enqueue(new Callback<CoachInfo>() {
            @Override
            public void onResponse(Call<CoachInfo> call, Response<CoachInfo> response) {
                coachinfo = response.body();
                coachName.setText(coachinfo.getRealname());
                coachIntro.setText(coachinfo.getIntroduction());
                String address = "http://172.24.10.175/workout/Uploads/" + coachinfo.getThumb();
                if (!coachinfo.getThumb().equals("")) {
                    Picasso.with(mContext).load(address).into(coachPhoto);
                }
               /* else {
                    coachPhoto.setImageResource(R.drawable.beijing);
                }*/

            }

            @Override
            public void onFailure(Call<CoachInfo> call, Throwable t) {

            }
        });
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (Tab01 == null) {
                    Tab01 = new DashiClassInfoFragment();
                    transaction.add(R.id.coach_frame, Tab01);
                } else {
                    transaction.show(Tab01);
                }

                break;
            case 1:
                if (Tab02 == null) {
                    Tab02 = new DashiWentiInfoFragment();
                    transaction.add(R.id.coach_frame, Tab02);
                } else {
                    transaction.show(Tab02);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }


    private void hideFragment(FragmentTransaction transaction) {
        if (Tab01 != null) {
            transaction.hide(Tab01);
        }
        if (Tab02 != null) {
            transaction.hide(Tab02);
        }
    }


    public void onClick(View view) {
        resetImgs();
        switch (view.getId()) {
            case R.id.coachkt:
                setSelect(0);
                break;
            case R.id.coachwt:
                setSelect(1);
                break;
            default:
                break;
        }

    }

    private void resetImgs() {
    }
    private void getTeaState(){
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.getTeaState(coach_id,userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    collect.setImageResource(R.drawable.collect2);
                }else{
                    collect.setImageResource(R.drawable.collect1);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
    private void collect(){
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.changeTeaState(userid,coach_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if(response.body()!=0){
                    Toast.makeText(CoachListActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                    collect.setImageResource(R.drawable.collect2);
                }
                else {
                    Toast.makeText(CoachListActivity.this,"关注失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }


}




